package ar.edu.unq.tpi.uis.tepe3.persistencia;


import org.hibernate.Session;

import ar.edu.unq.tpi.uis.tepe3.dominio.Folder;
import ar.edu.unq.tpi.uis.tepe3.dominio.PrivTypeR;
import ar.edu.unq.tpi.uis.tepe3.dominio.PrivTypeRW;

/**
 * Esta clase carga las tablas de la base y crea algunos datos iniciales.
 * Se ejecuta con el siguiente comando:
 * mvn -e exec:java -Dexec.mainClass="ar.edu.unq.tpi.uis.tepe3.persistencia.DataFixture" -Dexec.args="loadAll"
 *
 * @author hslavich
 */
public class DataFixture {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DataFixture fixt = new DataFixture();

        if (args[0].equals("loadAll")) {
        	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        	session.beginTransaction();

        	fixt.createAndStoreFolders(session);
        	fixt.createAndStorePrivTypes(session);
        	
        	session.getTransaction().commit();
        }

        HibernateUtil.getSessionFactory().close();
    }

    private void createAndStorePrivTypes(Session session) {
    	PrivTypeR typeR = new PrivTypeR();
    	session.save(typeR);
    	PrivTypeRW typeRW = new PrivTypeRW();
    	session.save(typeRW);
	}

	private void createAndStoreFolders(Session session) {

        Folder theFolder1 = new Folder();
        theFolder1.setName("folder1");
        session.save(theFolder1);
        
        Folder theFolder2 = new Folder();
        theFolder2.setName("folder2");
        theFolder1.addDaughter(theFolder2);
        session.save(theFolder2);

        Folder theFolder3 = new Folder();
        theFolder3.setName("folder3");
        theFolder2.addDaughter(theFolder3);
        session.save(theFolder3);

        //System.out.println("HIJAS: "+ theFolder2.getDaughters());
    }

}
